package org.example.controller.assets;

import lombok.extern.slf4j.Slf4j;
import org.example.common.ApiResponse;
import org.example.entity.task.AttackTask;
import org.example.service.impl.AttackTaskServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @Description
 * @Author 魏锴
 * @Date 2024/1/12
 */
@Slf4j
@Controller
@ResponseBody
@RequestMapping("/asset/task")
public class taskController {
    @Autowired
    AttackTaskServiceImpl attackTaskService;

    @PostMapping("/create")
    public ApiResponse insertAttackTask(AttackTask taskVO) {
        System.out.println(taskVO);
        try {
            if (attackTaskService.insertTask(taskVO)) return ApiResponse.ok("创建成功");
            else return ApiResponse.failure("创建失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("创建失败");
        }
    }

    @PostMapping("/update")
    public ApiResponse updateAttackTask(AttackTask taskVO) {
//        System.out.println(taskVO);
        try {
            if (attackTaskService.updateTask(taskVO)) return ApiResponse.ok("修改成功");
            else return ApiResponse.failure("修改失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("修改失败");
        }
    }

    @PostMapping("/delete/{id}")
    public ApiResponse deleteAttackTask(@PathVariable String id) {
        try {
            if (attackTaskService.deleteTask(Integer.parseInt(id))) return ApiResponse.ok("删除成功");
            else return ApiResponse.failure("删除失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("删除失败");
        }
    }

    @GetMapping("/list")
    public ApiResponse getAttackTask() {
        //获取组织信息树
        try {
            return ApiResponse.ok("获取成功", attackTaskService.getTaskList());
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("获取失败");
        }
    }

    @PostMapping("/publish/{id}")
    public ApiResponse publishTask(@PathVariable String id) {//发布任务
        try {
            if (attackTaskService.publishTask(Integer.parseInt(id))) return ApiResponse.ok("发布成功");
            else return ApiResponse.failure("发布失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("发布失败");
        }
    }

    @PostMapping("/tester/create")
    public ApiResponse insertTester(int task_id, int user_id) {
        try {
//            System.out.println(task_id + " | " + user_id);
            if (attackTaskService.insertTester(task_id, user_id)) return ApiResponse.ok("创建成功");
            else return ApiResponse.failure("创建失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("创建失败");
        }
    }

    @PostMapping("/tester/delete/{id}")
    public ApiResponse deleteTester(@PathVariable String id) {
        try {
            if (attackTaskService.deleteTester(Integer.parseInt(id))) return ApiResponse.ok("删除成功");
            else return ApiResponse.failure("删除失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("删除失败");
        }
    }

    /**
     * @return ApiResponse
     * @Desc 获得可使用的测试员信息
     */
    @GetMapping("/tester/info")
    public ApiResponse getAvailableUserInfo() {
        try {
            return ApiResponse.ok("获取成功", attackTaskService.getTesterInfo());
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("获取失败");
        }
    }
}
